package com.ponchware.classes.weapons
{
	import com.ponchware.classes.events.WeaponHitEvent;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Vector3D;
	
	import mx.core.UIComponent;

	[Event(name="hit", type="com.ponchware.classes.events.WeaponHitEvent")]
	public class Shell extends UIComponent
	{
		private const WEAPON_WIDTH:Number = 10;
		private const WEAPON_HEIGHT:Number = 5;
		
		public static const WEAPON_TYPE:String = "shell";
		public static const WEAPON_SPEED:Number = 20;
		private const GRAVITY:Number = .2;
		
		private var angle:Number;
		private var speed:Number;
		
		private var xspeed:Number;
		private var yspeed:Number;
		
		private var gameObjects:Array;
		
		private var img:Sprite;
		public function Shell(x:Number, y:Number, angle:Number, speed:Number, gameObjects:Array)
		{
			// We've been fired!
			super();
			this.x = x;
			this.y = y;
			this.angle = angle;
			this.speed = speed;
			this.gameObjects = gameObjects;
			
			// Calculate xspeed and yspeed
			xspeed = speed * Math.cos(angle);
			yspeed = 0 - speed * Math.sin(angle);
			
			this.addEventListener(Event.ENTER_FRAME, onMove);
		}
		
		protected override function updateDisplayList(unscaledWidth:Number, unscaledHeigh:Number):void
		{
			draw();
		}
		
		protected override function measure():void
		{
			super.measure();
			
			measuredWidth = WEAPON_WIDTH;
			measuredHeight = WEAPON_HEIGHT;
		}
		
		private function draw():void
		{
			// Draw shell
			graphics.clear();
			graphics.lineStyle(2);
			graphics.beginFill(0xFFFFFF);
			graphics.drawEllipse(0, 0, width, height);
			graphics.endFill();
			
			// Update angle
			this.rotationZ = angle;
		}
		
		// Event handlers
		
		public function onMove(e:Event):void
		{
			var p:Point = new Point(this.x + this.width/2, this.y + this.height);
			var hit:Boolean = false;
			var i:Number = 0;
			
			
			
				// Update our position
				x += xspeed;
				y += yspeed;
				
				// Update "gravity"
				yspeed += GRAVITY;
				
				// update angle
				var directionVector:Vector3D = new Vector3D(xspeed, yspeed);
				var horizonVector:Vector3D = new Vector3D(1,0);
				
				// Get the angle between our direction and the horizon (radians)
				var a:Number = Vector3D.angleBetween(directionVector, horizonVector);
				// Convert to degrees
				a = a * 180 / Math.PI;
				
				if ( (xspeed > 0 && yspeed < 0) || (xspeed < 0 && yspeed < 0) )
				{
					// Means we are travelling up and to the right or
					// up and left
					this.rotationZ = 360 - a;
				}
				else if ( (xspeed > 0 && yspeed > 0) || (xspeed < 0 && yspeed > 0) )
				{
					// down and to the right or 
					// down and to the left
					this.rotationZ = a;
				}
				
			while (!hit && i < gameObjects.length)
			{
				
				hit = gameObjects[i].hitTestPoint(p.x,p.y, true);
				i++;
			}
			
			
			// gameObjects[i-1] is the object we hit if hit = true
			if (hit || x < 0 || x > this.parentApplication.width || y > this.parentApplication.height)
			{
				y -= 2 * yspeed;
				this.removeEventListener(Event.ENTER_FRAME, onMove);
				dispatchEvent(new WeaponHitEvent(WeaponHitEvent.HIT, (hit ? gameObjects[i-1] : null)));
			}
			
			
			
			
			
			
		}
		
	}
}